/*
 * 
 *  This file is part of Sourceprojects.org Lycia Copyright 2010 noctarius
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 * 
 */
package org.sourceprojects.lycia.internal.validation;

import java.lang.annotation.Annotation;
import java.util.HashMap;
import java.util.Map;

import org.sourceprojects.lycia.ConstraintValidator;
import org.sourceprojects.lycia.exceptions.LyciaConfigurationException;

public class ConstraintValidatorFactory {
	private static final Map<Class<? extends ConstraintValidator<?>>, ConstraintValidator<?>> CACHED_INSTANCES = new HashMap<Class<? extends ConstraintValidator<?>>, ConstraintValidator<?>>();

	static {
		CACHED_INSTANCES.put(AssertFalseConstraintValidator.class,
				new AssertFalseConstraintValidator());
		CACHED_INSTANCES.put(AssertTrueConstraintValidator.class,
				new AssertTrueConstraintValidator());
		CACHED_INSTANCES.put(DecimalMaxConstraintValidator.class,
				new DecimalMaxConstraintValidator());
		CACHED_INSTANCES.put(DecimalMinConstraintValidator.class,
				new DecimalMinConstraintValidator());
		CACHED_INSTANCES.put(DigitsConstraintValidator.class,
				new DigitsConstraintValidator());
		CACHED_INSTANCES.put(EmptyConstraintValidator.class,
				new EmptyConstraintValidator());
		CACHED_INSTANCES.put(FutureConstraintValidator.class,
				new FutureConstraintValidator());
		CACHED_INSTANCES.put(MaxConstraintValidator.class,
				new MaxConstraintValidator());
		CACHED_INSTANCES.put(MinConstraintValidator.class,
				new MinConstraintValidator());
		CACHED_INSTANCES.put(NotEmptyConstraintValidator.class,
				new NotEmptyConstraintValidator());
		CACHED_INSTANCES.put(NotNullConstraintValidator.class,
				new NotNullConstraintValidator());
		CACHED_INSTANCES.put(NullConstraintValidator.class,
				new NullConstraintValidator());
		CACHED_INSTANCES.put(PastConstraintValidator.class,
				new PastConstraintValidator());
		CACHED_INSTANCES.put(PatternConstraintValidator.class,
				new PatternConstraintValidator());
		CACHED_INSTANCES.put(SizeConstraintValidator.class,
				new SizeConstraintValidator());
	}

	@SuppressWarnings("unchecked")
	public static final <A extends Annotation> ConstraintValidator<A> newInstance(
			final Class<? extends ConstraintValidator<?>> clazz) {

		try {
			final ConstraintValidator<?> v = CACHED_INSTANCES.get(clazz);

			if (v != null) {
				return (ConstraintValidator<A>) v;
			} else {
				return (ConstraintValidator<A>) clazz.newInstance();
			}

		} catch (final InstantiationException e) {
			throw new LyciaConfigurationException(e);
		} catch (final IllegalAccessException e) {
			throw new LyciaConfigurationException(e);
		}
	}
}
